package com.zyddd.utils;

import java.util.Random;

public class RandomCaptcha {
    public static String str;

    static {
        StringBuilder _str = new StringBuilder();
        for (char c = '1'; c <= '9'; c++) _str.append(c);
        for (char c = 'a'; c <= 'z'; c++) _str.append(c);
        for (char c = 'A'; c <= 'Z'; c++) _str.append(c);
        str = _str.toString();
    }

    public static String get(int length) {
        if (length <= 0 || length > 50) {
            throw new RuntimeException("验证码长度只能在1-50之间");
        }
        StringBuilder res = new StringBuilder();
        Random random = new Random();
        for (int i = 1; i <= length; i++) {
            res.append(str.charAt(random.nextInt(str.length())));
        }
        return res.toString();
    }
}
